local FileSystem = Tubes.FileSystem
FileSystem._RealFolder = {}
local RealFolder = FileSystem._RealFolder
RealFolder.__index = RealFolder
setmetatable (RealFolder, Tubes.FileSystem._Folder)

FileSystem.RealFolder = Tubes.MakeConstructor (RealFolder)

-- RealFolders take the full path as their constructor argument
function RealFolder:ctor (name)
	name = name:gsub ("\\", "/")
	self.RealPath = name
	
	local parts = string.Explode ("/", name)
	name = parts [#parts]
	FileSystem._Folder.ctor (self, name)
	
	self:MarkPopulated ()
	self.ChildrenPopulated = false
end

function RealFolder:CreateFolderDirect (ply, name, ignoreLast)
	if not self:PlayerHasPermission (ply, "Create Folder") then
		return nil
	end
	if self.RealPath:sub (1, 5):lower () == "data/" then
		file.CreateDir (self.RealPath:sub (6) .. "/" .. name .. "/")
	end
	if file.Exists (self.RealPath .. "/" .. name, true) then
		return self:AddItem (FileSystem.RealFolder (self.RealPath .. "/" .. name))
	end
	return nil
end

function RealFolder:GetChildIterator (ply, callback)
	if not self:PlayerHasPermission (ply, "View Folder") then
		return
	end
	if not self.ChildrenPopulated then
		file.TFind (self.RealPath .. "/*", function (_, folders, files)
			self.ChildrenPopulated = true
			for _, fileName in ipairs (folders) do
				self:AddItem (FileSystem.RealFolder (self.RealPath .. "/" .. fileName))
			end
			if file.IsDir (self.RealPath .. "/.svn", true) then
				self:AddItem (FileSystem.RealFolder (self.RealPath .. "/.svn"))
			end
			for _, fileName in ipairs (files) do
				self:AddItem (FileSystem.RealFile (self.RealPath .. "/" .. fileName))
			end
			FileSystem._Folder.GetChildIterator (self, ply, callback)
		end)
	else
		FileSystem._Folder.GetChildIterator (self, ply, callback)
	end
end

function RealFolder:GetRealPath ()
	return self.RealPath
end

function RealFolder:IsReal ()
	return true
end

function RealFolder:Refresh (ply)
	self.ChildrenPopulated = false
	self:GetChildIterator (ply, function () end)
end